﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Infraestrutura.Repositorios.SQL
{
    public class BaseSQL
    {

        private String _connectionString;

        protected String ConnectionString
        {
            get { return _connectionString; }
            set { _connectionString = value; }
        }

        protected DataSet ObterDataSet(String commandText)
        {
            Dictionary<String, Object> parametros = new Dictionary<String, Object>();
            return ObterDataSet(commandText, parametros);
        }

        protected DataSet ObterDataSet(String commandText, Dictionary<String, Object> parametros)
        {
            DataSet dataSet = new DataSet();

            using (SqlConnection sqlConnection = new SqlConnection(_connectionString))
            {
                String sqlCommand = commandText;
                
                SqlCommand command = new SqlCommand(commandText, sqlConnection);

                foreach(KeyValuePair<String, Object> param in parametros)
                {
                    command.Parameters.AddWithValue(param.Key, param.Value);
                }

                sqlConnection.Open();
                SqlDataAdapter adapter = new SqlDataAdapter(command);

                adapter.Fill(dataSet);
            }

            return dataSet;
        }
    }
}
